package com.example.databasepj.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResponseEntity<Object> handleException(Exception ex) {
        if (ex instanceof BusinessException) {
            BusinessException be = (BusinessException) ex;
            return new ResponseEntity<>(be.getErrorCode(), HttpStatus.BAD_REQUEST);
        }
        ex.printStackTrace();
        //可以考虑使用异常日志，但这个PJ暂不需要

        // 处理其他类型的异常
        return new ResponseEntity<>(ErrorCode.SYSTEM_ERROR, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
